package com.learner.course.controller.admin;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.course.service.TeacherService;
import com.learner.pojo.course.Teacher;
import com.learner.pojo.course.expand.PageVariableTeacher;
import com.learner.pojo.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
* @author ice2020x
* @Date: 2023/3/20
* @Description:  讲师管理
**/
@Slf4j
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    /**
     * 获取讲师列表
     *
     * @return 讲师列表
     */
    @GetMapping("/list")
    public List<Teacher> finAll() {
        return teacherService.list();
    }

    /**
     * 根据id删除
     *
     * @param id id
     */
    @DeleteMapping("/remove/{id}")
    public void deleteById(@PathVariable("id") String id) {
        teacherService.removeAvatarById(id);
        teacherService.removeById(id);
    }

    /**
     * 根据条件查询
     *
     * @param pathVariable 参数
     * @return 结果
     */
    @PostMapping("/findAll/condition")
    public PageResult<Teacher> listPage(@RequestBody PageVariableTeacher pathVariable) {
        return teacherService.listPageByPathVariable(pathVariable);
    }

    /**
     * 保存
     *
     * @param teacher teacher
     * @return 结果
     */
    @PostMapping("/save")
    public Teacher save(@RequestBody Teacher teacher) {
        return teacherService.save(teacher);
    }


    /**
     * 根据id获取讲师信息
     *
     * @param id 讲师id
     * @return 结果
     */
    @GetMapping("/getById/{id}")
    public Teacher getTeacherById(@PathVariable("id") String id) {
        return teacherService.getTeacherById(id);
    }

    /**
     * 根据id列表删除（逻辑删除）
     *
     * @param ids 讲师id列表
     */
    @DeleteMapping("/delete/batch")
    public void deleteByIds(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw ExceptionUtil.createException(RestException.class, 2000005);
        }
        teacherService.deleteByIds(ids);
    }


    /**
     * 根据id列表删除（真实删除）
     *
     * @param ids 讲师id列表
     */
    @DeleteMapping("/delete/batch/authentic")
    public void deleteByIdsAuthentic(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw ExceptionUtil.createException(RestException.class, 2000005);
        }
        teacherService.deleteAuthenticByIds(ids);
    }

    /**
     * 根据关键字查询（名字列表）
     *
     * @param key 关键字
     * @return 结果
     */
    @GetMapping("/list/name/{key}")
    public List<Map<String, Object>> getListNameByKey(@PathVariable("key") String key) {
        System.out.println(key);
        return teacherService.getListNameByKey(key);
    }

    /**
     * 根据关键字查询（讲师列表）+状态位
     *
     * @param key   关键字
     * @param state 状态
     * @return 结果
     */
    @GetMapping("/list/teacher/{key}/{state}")
    public List<Teacher> getListTeacherByName(@PathVariable("key") String key, @PathVariable("state") Integer state) {
        return teacherService.getTeacherListByName(key, state);
    }


    /**
     *  根据状态查询
     *
     * @param state 状态
     * @return 结果
     */
    @GetMapping("/list/{state}")
    public List<Teacher> getTeacherListByState(@PathVariable("state") Integer state) {
        return teacherService.getTeacherListByState(state);
    }

}
